package com.lc.question.controller;

import com.lc.question.pojo.MyTab;
import com.lc.question.pojo.Plan;
import com.lc.question.pojo.QuestionNaire;
import com.lc.question.pojo.UserTab;
import com.lc.question.service.impl.PlanServiceImpl;
import com.lc.question.service.impl.QuestionNaireServiceImpl;
import com.lc.question.service.impl.QuestionServiceImpl;
import com.lc.question.service.impl.UserTabServiceImpl;
import com.lc.question.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@Api(tags = "UserTabController|问卷表接口类")
@RequestMapping("/usertab")
public class UserTabController {

    @Autowired
    UserTabServiceImpl userTabService;
    @Autowired
    QuestionNaireServiceImpl questionNaireService;
    @Autowired
    QuestionServiceImpl questionService;
    @Autowired
    PlanServiceImpl planService;

    //查询用户的所有问卷
    @ApiOperation(value = "tabAll(查询用户的所有问卷)")
    @PostMapping("/tabAll")
    @ResponseBody
    public List<MyTab> userTablist(String userId){
        List<UserTab> userTabs = userTabService.userTabAll(userId);
        List<MyTab> MyTabs = new ArrayList<>();


        for (UserTab userTab : userTabs) {
            int plan_Id = userTab.getPlan_Id();
            Plan plan = planService.findPlanByPid(plan_Id);
            int tab_id = plan.getTab_Id();
            QuestionNaire questionNaire = questionNaireService.findQuestionNaireByTabId(tab_id);
            MyTab myTab = new MyTab(userTab,questionNaire);
            MyTabs.add(myTab);
        }

        return MyTabs;
    }

    //查询发布人的所有问卷
    @ApiOperation(value = "pubtabAll(查询发布人的所有问卷)")
    @PostMapping("/pubtabAll")
    @ResponseBody
    public List<QuestionNaire> pubTablist(String pub_Id){
        List<QuestionNaire> queNaires = questionNaireService.findQuestionNaireByPubId(pub_Id);
        return queNaires;
    }

    //更改用户参与状态
    @ApiOperation(value = "update_uid(更改用户参与状态)")
    @PostMapping("/update_uid")
    public void updateMyTab(String user_Id,int plan_Id) {
        userTabService.updateMyTab(user_Id,plan_Id);
    }

    //更改问卷表的状态为已结束
    @ApiOperation(value = "update_pid(更改问卷表的状态为已结束)")
    @PostMapping("/update_pid")
    public void updateTabSur(int tab_Id) {
        userTabService.updateTabSur(tab_Id);
    }

    //查询有关该问卷的所有用户
    @ApiOperation(value = "userAll(查询有关该问卷的所有用户)")
    @PostMapping("/userAll")
    @ResponseBody
    public List<UserTab> findTab_TId(int tab_Id) {
        return userTabService.findTab_TId(tab_Id);
    }

    //查询用户表（参与和问卷状态改变时）
    @ApiOperation(value = "查询用户表（参与和问卷状态改变时）")
    @PostMapping("/tab_surpar")
    @ResponseBody
    public List<MyTab> findAllMyTab_SurPar(@RequestBody UserTab oldUserTab) {
        List<UserTab> userTabs = userTabService.findUserChange_SurPar(oldUserTab);
        List<MyTab> MyTabs = new ArrayList<>();

        for (UserTab userTab : userTabs) {
            int plan_Id = userTab.getPlan_Id();
            Plan plan = planService.findPlanByPid(plan_Id);
            int tab_id = plan.getTab_Id();
            QuestionNaire questionNaire = questionNaireService.findQuestionNaireByTabId(tab_id);
            MyTab myTab = new MyTab(userTab,questionNaire);
            MyTabs.add(myTab);
        }
        return MyTabs;
    }

}
